package com.blt.car;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blt.car.base.BaseActivity;
import com.blt.car.base.BaseFragment;
import com.blt.car.base.ViewHolder;
import com.blt.car.home.HomeFragment;
import com.blt.car.journey.JourneyFragment;
import com.blt.car.login.InitActivity;
import com.blt.car.me.MeFragment;
import com.blt.car.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import blt.app_core.api.base.CoreManager;
import blt.app_core.api.login.LoginApi;
import blt.app_core.api.other.OutApi;

/**
 * Created by xgp on 2017/6/13.
 */

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private NoScrollViewPager viewPager;
    private static final int INDEX_HOME = 0;
    private static final int INDEX_JOURNEY = 1;
    private static final int INDEX_ME = 2;

    private List<BaseFragment> fragList = new ArrayList<>();

    private TextView mHome;
    private TextView mJourney;
    private TextView mMe;

    @Override
    protected void initData() {
        super.initData();
        initFragment();
        boolean isLogin = CoreManager.getCore(LoginApi.class).isLogin();
        if (isLogin) {
            TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
            String szImei = TelephonyMgr.getDeviceId();
            CoreManager.getCore(OutApi.class).registPush(szImei);
        }
    }

    private void initFragment() {
        HomeFragment hf = new HomeFragment();
        MeFragment mf = new MeFragment();
        JourneyFragment jf = new JourneyFragment();
        fragList.add(hf);
        fragList.add(jf);
        fragList.add(mf);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(ViewHolder holder) {
        mHome = holder.get(R.id.main_tab_home);
        mJourney = holder.get(R.id.main_tab_journey);
        mMe = holder.get(R.id.main_tab_me);
        mHome.setSelected(true);
        holder.setOnClickListener(this, R.id.main_tab_home, R.id.main_tab_journey, R.id.main_tab_me);

        viewPager = holder.get(R.id.main_viewpager);
        TabFragAdapter tfa = new TabFragAdapter(getSupportFragmentManager());
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(tfa);
        viewPager.setScrollEnabled(false);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_tab_home:
                viewPager.setCurrentItem(INDEX_HOME);
                mHome.setSelected(true);
                mJourney.setSelected(false);
                mMe.setSelected(false);
                mHome.setTextColor(getResources().getColor(R.color.text_color_blue));
                mJourney.setTextColor(getResources().getColor(R.color.text_color_gray));
                mMe.setTextColor(getResources().getColor(R.color.text_color_gray));
                break;
            case R.id.main_tab_journey:
                viewPager.setCurrentItem(INDEX_JOURNEY);
                mHome.setSelected(false);
                mJourney.setSelected(true);
                mMe.setSelected(false);
                mHome.setTextColor(getResources().getColor(R.color.text_color_gray));
                mJourney.setTextColor(getResources().getColor(R.color.text_color_blue));
                mMe.setTextColor(getResources().getColor(R.color.text_color_gray));
                break;
            case R.id.main_tab_me:
                viewPager.setCurrentItem(INDEX_ME);
                mHome.setSelected(false);
                mJourney.setSelected(false);
                mMe.setSelected(true);
                mHome.setTextColor(getResources().getColor(R.color.text_color_gray));
                mJourney.setTextColor(getResources().getColor(R.color.text_color_gray));
                mMe.setTextColor(getResources().getColor(R.color.text_color_blue));
                break;

        }

    }

    class TabFragAdapter extends FragmentPagerAdapter {

        public TabFragAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragList.get(position);
        }

        @Override
        public int getCount() {
            return fragList.size();
        }
    }


}
